/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import bean.ExamMarkBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alex
 */
public class ControlMark extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ExamMarkBean exam=new ExamMarkBean();
        try{
            if(request.getParameter("bt").equals("Insert")) {
                int stuID=request.getIntHeader("stuID");
                int suID=request.getIntHeader("suID");
                String maDate=request.getParameter("maDate");
                float maMark=Float.parseFloat(request.getParameter("maMark"));

                boolean rs=exam.InsertExamMark(stuID, suID, maMark, maDate);
                if(rs==true){
                    response.sendRedirect("Mark Redirect Page");
                }else
                    out.println("<Mark Redirect Page>Error!</a>");
            }
            if(request.getParameter("bt").equals("Delete")) {
                String maID=request.getParameter("maID");
                boolean rs=exam.deleteExamMark(Integer.parseInt(maID));
                response.sendRedirect("index.jsp?page=ResultDeclarationBoard&url=Mark");
            }
            if(request.getParameter("bt").equals("Edit")){
                int stuID=request.getIntHeader("stuID");
                String maDate=request.getParameter("maDate");
                float maMark=Float.parseFloat(request.getParameter("maMark"));
                HttpSession session=request.getSession(true);
                session.setAttribute("stuID", stuID);
                session.setAttribute("maDate", maDate);
                session.setAttribute("mark", maMark);
                response.sendRedirect("Guest Mark Page Redirect");
            }
            if(request.getParameter("bt").equals("Update")) {
                int stuID=request.getIntHeader("stuID");
                int suID=request.getIntHeader("suID");
                String maDate=request.getParameter("maDate");
                float maMark=Float.parseFloat(request.getParameter("maMark"));
                boolean rs=exam.updateMark(stuID, suID, maMark, maDate);
                if(rs==true){
                    HttpSession session = request.getSession();
                    session.removeAttribute("stuID");
                    session.removeAttribute("maDate");
                    session.removeAttribute("maMark");
                    response.sendRedirect("Guest Mark Page Redirect");
                }else
                    out.println("<a href='index.jsp?page=ResultDeclarationBoard&url=Mark'>Insert fail back...!</a>");
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getServletPath().equals("/ControlMark")) {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            ExamMarkBean exam = new ExamMarkBean();
            String date = request.getParameter("txtdate");
            try {
                request.getSession().setAttribute("str", exam.getListExamMark());
                response.sendRedirect("index.jsp");
            } finally {
                out.close();
            }

        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
